package com.superman.supermarket.client.view.comment;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.Widget;
import com.superman.mvpframe.client.mvp.IPageViewPresenter;
import com.superman.supermarket.client.composite.IUIModule;
import com.superman.supermarket.client.composite.comment.CommentSkuModule;

/**
 * 评价ui实现类
 * 
 * @author superman
 * @version 2018年8月15日下午10:53:24
 */
public class CommentViewImpl extends Composite implements CommentView {

	interface CommentViewImplUiBinder extends UiBinder<Widget, CommentViewImpl> {
	}

	private static CommentViewImplUiBinder uiBinder = GWT.create(CommentViewImplUiBinder.class);

	private CommentViewPresenter presenter;

	@UiField
	FlowPanel skulistpanel;

	/**
	 * 构造方法
	 * 
	 * @author superman
	 * @version 2018年8月15日 下午10:53:21
	 */
	public CommentViewImpl() {
		initWidget(uiBinder.createAndBindUi(this));
	}

	private List<Map<String, Object>> getCommentSkuList() {
		List<Map<String, Object>> commentSkuList = new ArrayList<Map<String, Object>>();
		for (int i = 0; i < skulistpanel.getWidgetCount(); i++) {
			Widget widget = skulistpanel.getWidget(i);
			if (widget instanceof IUIModule) {
				IUIModule module = (IUIModule) widget;
				commentSkuList.add(module.getData());
			}
		}
		return commentSkuList;
	}

	@Override
	public void init() {
		// TODO Auto-generated method stub

	}

	@Override
	public void setPageViewPresenter(IPageViewPresenter presenter) {
		this.presenter = (CommentViewPresenter) presenter;
	}

	@Override
	public void setToCommentSkuList(List<Map<String, Object>> list) {
		for (Map<String, Object> map : list) {
			CommentSkuModule commentSkuModule = new CommentSkuModule();
			commentSkuModule.setData(map);
			skulistpanel.add(commentSkuModule);
		}
	}

	@UiHandler("submitbtn")
	void submit(ClickEvent event) {
		presenter.submit(getCommentSkuList());
	}

}
